package co.gov.medellin.ssti.sub.paoyer.administration.web.action;

import java.io.IOException;
import java.util.Collection;
import java.util.Vector;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;

import co.gov.medellin.ssti.sub.paoyer.administration.AdministrationMngr;
import co.gov.medellin.ssti.sub.paoyer.administration.model.vo.AuthorizedByBranchVO;
import co.gov.medellin.ssti.sub.paoyer.administration.model.vo.ContractVO;
import co.gov.medellin.ssti.sub.paoyer.administration.model.vo.CostCenterByAuthorizedVO;
import co.gov.medellin.ssti.sub.paoyer.administration.model.vo.FDClassVO;
import co.gov.medellin.ssti.sub.paoyer.administration.model.vo.FDTypeVO;
import co.gov.medellin.ssti.sub.paoyer.administration.model.vo.FDVO;
import co.gov.medellin.ssti.sub.paoyer.administration.model.vo.UserVO;
import co.gov.medellin.ssti.sub.paoyer.administration.model.vo.WarehouseByBranchVO;
import co.gov.medellin.ssti.sub.paoyer.administration.web.form.FDForm;
import co.gov.medellin.ssti.sub.paoyer.common.exception.SystemException;
import co.gov.medellin.ssti.sub.paoyer.common.exception.UserException;

public class SearchFDAction extends Action {
	/**
	 * This is the main action called from the Struts framework.
	 * 
	 * @param mapping
	 *            The ActionMapping used to select this instance.
	 * @param form
	 *            The optional ActionForm bean for this request.
	 * @param request
	 *            The HTTP Request we are processing.
	 * @param response
	 *            The HTTP Response we are processing.
	 */
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException {

		if (request.getSession().getAttribute("userInfo") == null) {
			response.sendRedirect("/crm/index.jsp");
		}

		FDForm fdForm = (FDForm) form;
		ActionMessages messages = new ActionMessages();
		AdministrationMngr delegate = new AdministrationMngr();
		HttpSession session = request.getSession();
		UserVO user = (UserVO) session.getAttribute("userInfo");
		FDVO fd = new FDVO();

		try {

			// Cargar informacion de sucursales
			AuthorizedByBranchVO branch = new AuthorizedByBranchVO();
			branch.setAuthorizedInternalCode(user.getAuthorizedId());
			Collection<AuthorizedByBranchVO> branches = delegate
					.findAuthorizedBranchByCriterea(branch);

			if (branches != null && branches.size() > 0) {
				request.setAttribute("branches", branches);
			}

			// Cargar informacion de clases de fondos documentales

			Collection<FDClassVO> classes = delegate
					.findFDClassByCriterea(new FDClassVO());

			if (classes != null && classes.size() > 0) {
				request.setAttribute("classes", classes);
			}

			// validar si se selecciono una sucursal
			if (request.getParameter("warehouse") != null) {
				AuthorizedByBranchVO branch1 = new AuthorizedByBranchVO();
				branch1.setAuthorizedInternalCode(user.getAuthorizedId());
				branch1.setBranchInternalCode(fdForm.getBranchId());
				Vector<AuthorizedByBranchVO> branches1 = new Vector<AuthorizedByBranchVO>(
						delegate.findAuthorizedBranchByCriterea(branch1));
				request.setAttribute("selectedBranch",
						(AuthorizedByBranchVO) branches1.firstElement());
				fd.setBranchId(fdForm.getBranchId());
				request.setAttribute("actualRequest", fd);
				// Cargar informacion de contratos
				ContractVO contract = new ContractVO();
				contract.setUserId(user.getAuthorizedId());
				Collection<ContractVO> contracts = delegate
						.findContractByCriterea(contract);

				if (contracts != null && contracts.size() > 0) {
					request.setAttribute("contracts", contracts);
				}
				// Cargar informacion de Bodegas
				WarehouseByBranchVO warehouse = new WarehouseByBranchVO();
				warehouse.setBranchInternalCode(fd.getBranchId());

				Collection<WarehouseByBranchVO> warehouses = delegate
						.findWarehouseBranchByCriterea(warehouse);

				if (warehouses != null && warehouses.size() > 0) {
					request.setAttribute("warehouses", warehouses);
				}

				// Cargar informacion de centros de costo
				CostCenterByAuthorizedVO center = new CostCenterByAuthorizedVO();
				center.setAuthorizedInternalCode(user.getAuthorizedId());
				Collection<CostCenterByAuthorizedVO> centers = delegate
						.findCostCenterAuthorizedByCriterea(center);

				if (centers != null && centers.size() > 0) {
					request.setAttribute("centers", centers);
				}

			}

			if (request.getParameter("initial") != null) {
				if (request.getParameter("initial").trim()
						.equalsIgnoreCase("types")) {
					FDClassVO clas = new FDClassVO();
					clas.setCode(fdForm.getFdClassId());
					Vector<FDClassVO> classes1 = new Vector<FDClassVO>(
							delegate.findFDClassByCriterea(clas));
					request.setAttribute("selectedClass",
							(FDClassVO) classes1.firstElement());

					// Cargar informacion de tipos de fondo documental
					FDTypeVO type = new FDTypeVO();
					type.setFdClassCode(fdForm.getFdClassId());
					Collection<FDTypeVO> types = delegate
							.findFDTypeByCriterea(type);

					if (types != null && types.size() > 0) {
						request.setAttribute("types", types);
					}
				} else {
					if (request.getParameter("consultType").trim()
							.equalsIgnoreCase("1")) {
						fd = new FDVO();
						fd.setBranchId(fdForm.getBranchId());
						fd.setContract(fdForm.getContract());
						fd.setCostCenterId(fdForm.getCostCenterId());
						fd.setWarehouseId(fdForm.getWarehouseId());

					} else if (request.getParameter("consultType").trim()
							.equalsIgnoreCase("2")) {
						fd = new FDVO();
						fd.setFd(fdForm.getFd());
						fd.setFdType(fdForm.getFdType());

					} else {
						fd = new FDVO();
						fd.setFdClassId(fdForm.getFdClassId());
						fd.setFdTypeId(fdForm.getFdTypeId());

					}
					fd.setAuthorizedCode(user.getAuthorizedId());
					fd.setCutDate(fdForm.getCutDate());
					Collection<FDVO> FDList = delegate
							.findInventoryByCriterea(fd);
					session.removeAttribute("actualRequest");

					if (FDList != null && FDList.size() > 0) {
						request.setAttribute("FDList", FDList);
					}
				}
			}

		} catch (UserException ue) {
			messages.add("msgWarn", new ActionMessage("loginForm.message."
					+ ue.getExceptionVO().getId()));
			saveMessages(request, messages);
			return mapping.getInputForward();

		} catch (SystemException se) {
			messages.add("msgError", new ActionMessage("loginForm.message."
					+ se.getExceptionVO().getId()));
			saveMessages(request, messages);
			return mapping.getInputForward();
		}
		return mapping.findForward("success");
	}
}